package sloutiondemo;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2024-01-18
 * Time: 23:36
 */
class Solution {


    //判断是否为操作符
    private boolean isOperation(String str) {
        if(str.equals("+") ||str.equals("-") ||str.equals("*") ||str.equals("/")) {
            return true;
        }
        return false;
    }


    public int evalRPN(String[] tokens) {


        Stack<Integer> stack = new Stack<>();
        //1.先遍历字符串
        for(int i = 0; i < tokens.length; i++) {

            String str = tokens[i];
            //如果为操作符
            if(isOperation(str)) {
                int num2 = stack.pop();
                int num1 = stack.pop();
                if(str.equals("+")) {
                    stack.push(num1 + num2);
                }else if(str.equals("-")) {
                    stack.push(num1 - num2);
                }else if(str.equals("*")) {
                    stack.push(num1 * num2);
                }else if(str.equals("/")) {
                    stack.push(num1 / num2);
                }

            }else {
                stack.push(Integer.valueOf(str));
            }
        }
        return stack.pop();
    }
}